package mia.beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

import org.hibernate.Session;

import mia.dao.UserEntityDAO;
import mia.entity.UserEntity;
import mia.util.DigesterUtil;
import mia.util.HibernateManager;

@ManagedBean(name="userEditAdminBean")
@RequestScoped
public class UserEditByAdminBean {

	@ManagedProperty("#{userLoginBean.user}")
	private UserEntity user;
	
	@ManagedProperty("#{searchUsersBean.selectedUser}")
	private UserEntity editUser;
	
	private String passwordAdmin;
	private String passwordNew;
	private String passwordNew2;
	
	private String editStatus = "";

	public UserEntity getUser() {
		return user;
	}
	public void setUser(UserEntity user) {
		this.user = user;
	}
	public String getPasswordAdmin() {
		return passwordAdmin;
	}
	public void setPasswordAdmin(String passwordAdmin) {
		this.passwordAdmin = passwordAdmin;
	}
	public String getPasswordNew2() {
		return passwordNew2;
	}
	public void setPasswordNew2(String passwordNew2) {
		this.passwordNew2 = passwordNew2;
	}	
	public String getPasswordNew() {
		return passwordNew;
	}
	public void setPasswordNew(String passwordNew) {
		this.passwordNew = passwordNew;
	}
	public String getEditStatus() {
		return editStatus;
	}
	public void setEditStatus(String editStatus) {
		this.editStatus = editStatus;
	}
	public UserEntity getEditUser() {
		return editUser;
	}
	public void setEditUser(UserEntity editUser) {
		this.editUser = editUser;
	}
	public void tryEdit()
	{
		if(DigesterUtil.checkPassword(passwordAdmin, user.getPassword()))
		{
			if(passwordNew2.equals(passwordNew))
			{
				if(!UserEntityDAO.checkIfUserExists("", user.getEmail()))
				{
					String passwordNewEncrypted = DigesterUtil.getDigest(passwordNew);
					editUser.setPassword(passwordNewEncrypted);
					
					Session session = HibernateManager.getSession();
					session.beginTransaction();
					UserEntity u = UserEntityDAO.getUserByLogin(session, editUser.getLogin());
					
					if(!u.getEmail().equals(editUser.getEmail()))
						u.setEmail(editUser.getEmail());
					
					if(!u.getFirstName().equals(editUser.getFirstName()))
						u.setFirstName(editUser.getFirstName());
					
					if(!u.getLastName().equals(editUser.getLastName()))
						u.setLastName(editUser.getLastName());
					
					if(!u.getPassword().equals(passwordNewEncrypted))
						u.setPassword(passwordNewEncrypted);
					
					if(!u.getPhoneNumber().equals(editUser.getPhoneNumber()))
						u.setPhoneNumber(editUser.getPhoneNumber());
					
					if(!u.isAdmin() == editUser.isAdmin())
						u.setAdmin(editUser.isAdmin());
					
					if(!u.isActive() == editUser.isActive())
						u.setActive(editUser.isActive());
					
					try
					{
						session.save(u);
						session.getTransaction().commit();
						this.setEditStatus("EDYTOWANO POMY�LNIE");
					}
					catch(Exception ex)
					{
						this.setEditStatus("B��D");
						session.getTransaction().rollback();
					}
					finally
					{
						session.close();
					}
				}
				else
				{
					this.setEditStatus("ADRES EMAIL ZAJETY");
				}
			}
			else
			{
				this.setEditStatus("NOWE HAS�O NIE ZGADZA SI�");
			}
		}
		else
		{
			this.setEditStatus("B��DNE HAS�O ADMINISTRATORA");
		}
	}
	
}
